package cz.possoft.client.utils;

import java.awt.Color;

/**
 * Pomocne pole, ktere drzi informaci o slozkach barvy.
 * Navic umoznuje vypocitat rozdil barev k postupnemu mizeni a
 * objevovani figurek. 
 *  
 * @author mara
 */
public class RGBHolder {
	
	// pocet kroku k prekresleni jednotlivych figurek
	public static final int COUNT = 20;	
	
	// tato hodnota uchovava v sobe barvu, ktera je ulozena jako double kvuli operacim pricitani.
	private double r,g,b;
	
	public RGBHolder(int color) {
		setRGB(color);		
	}
	
	public RGBHolder(double r, double g, double b) {
		this.r= r;
		this.g = g;
		this.b = b;
	}	
	
	// z barvy nastavi jednotlive slozky
	public void setRGB(int color) {		
		r = (double)((color & 0x00ff0000) >> 16);
		g = (double)((color & 0x0000ff00) >> 8);
		b = (color & 0x000000ff);		
	}
	
	// z jednotlivych slozek vraci barvu
	public int getRGB() {
		Color c = new Color((int)r, (int)g, (int)b);
		return c.getRGB();
	}	    
	
	public double getR() {
		return r;
	}

	private void setR(double r) {
		this.r = r;
	}

	private double getG() {
		return g;
	}

	private void setG(double g) {
		this.g = g;
	}

	private double getB() {
		return b;
	}

	private void setB(double b) {
		this.b = b;
	}

	// vraci RGBHolder, ktery z base (barva policka sachovnice) a pixel(barva pixelu)
	// zjisti pricitani - vzdy jeden krok pro pricitani jednotlivych barev.
	public static RGBHolder getRGBDifferenceForBaseAndPixel(RGBHolder baseHolder, RGBHolder pixelHolder) {
           double rBase = baseHolder.getR();
           double gBase = baseHolder.getG();
           double bBase = baseHolder.getB();
           
           double rPixel = pixelHolder.getR();
           double gPixel = pixelHolder.getG();
           double bPixel = pixelHolder.getB();
           
           double rDif = (rBase - rPixel) / COUNT;
           double gDif = (gBase - gPixel) / COUNT;
           double bDif = (bBase - bPixel) / COUNT;
           return new RGBHolder(rDif,gDif,bDif);
	}
	
	// zmeni barvy prictenim RGBHolderu k tomuto
	public RGBHolder addRGBHolderToThis(RGBHolder differ) {
		setR(getR() + differ.getR());
		setG(getG() + differ.getG());
		setB(getB() + differ.getB());
		return this;
	}
	
}
